package com.duowei.diandan.util;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class FileUtils {
    private static final String TAG = "FileUtils";

    public static void initWelcomeFolder() {
        File file = new File(Environment.getExternalStorageDirectory(), "/Dw/welcome");
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            createNoMediaFile(file);
        } catch (IOException e) {
            Log.e(TAG, "创建 /Dw/welcome/.nomedia 文件失败", e);
        }
    }

    public static void createNoMediaFile(File dir) throws IOException {
        File noMediaFile = new File(dir, ".nomedia");
        if (!noMediaFile.exists()) {
            if (noMediaFile.createNewFile()) {
                Log.d(TAG, dir.getAbsolutePath() + "目录下创建.nomedia文件成功");
            } else {
                Log.d(TAG, dir.getAbsolutePath() + "目录下创建.nomedia文件失败");
            }
        }
    }

    /**
     * 删除目录
     *
     * @param folder
     */
    public static void deleteFolder(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFolder(f);
                } else {
                    f.delete();
                }
            }
        }
        folder.delete();
    }
}
